package com.demo.controller.flighttickets;

/**
 *  机票管理
 */

import com.demo.common.model.Flighttickets;
import com.demo.common.model.Flighttime;
import com.demo.controller.flighttime.FlightTimeValidator;
import com.demo.interceptor.WebInterceptor;
import com.demo.service.CodeService;
import com.demo.service.FlightTimeService;
import com.demo.service.FlightticketsService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;

@Before(WebInterceptor.class)
public class FlightticketsController extends Controller {


    @Inject
    FlightticketsService flightticketsService;

    @Inject
    CodeService codeService;

    @Inject
    FlightTimeService flightTimeService;

    public void index() {
        Kv cond=Kv.create();
        if(StrKit.notBlank(getPara("flighttimeno"))){
            cond.set("b.flighttimeno",getPara("flighttimeno"));
        }
        if(StrKit.notBlank(getPara("airno"))){
            cond.set("b.airno",getPara("airno"));
        }
        if(StrKit.notBlank(getPara("begin"))){
            cond.set("b.begin",getPara("begin"));
        }
        if(StrKit.notBlank(getPara("end"))){
            cond.set("b.end",getPara("end"));
        }
        setAttr("flightticketsPage", flightticketsService.paginate(getParaToInt(0, 1), 10,Kv.by("cond",cond)));
        render("flighttickets.html");
    }

    public void add() {
        setAttr("flighttimes",flightTimeService.findByVerify());
        setAttr("tickettypes",codeService.findByCategory("tickettype"));
    }

    /**
     * save 与 update 的业务逻辑在实际应用中也应该放在 serivce 之中，
     * 并要对数据进正确性进行验证，在此仅为了偷懒
     */
    @Before(FlightticketsValidator.class)
    public void save() {
        Flighttickets flighttickets=getBean(Flighttickets.class);

        Flighttime flighttime=this.flightTimeService.findById(flighttickets.getFlighttimeid());

        flighttickets.setFlighttimeno(flighttime.getFlighttimeno());
        flighttickets.save();
        redirect("/flighttickets");
    }

    public void edit() {
        setAttr("flighttimes",flightTimeService.findByVerify());
        setAttr("tickettypes",codeService.findByCategory("tickettype"));
        setAttr("flighttickets", flightticketsService.findById(getParaToInt()));
    }

    /**
     * save 与 update 的业务逻辑在实际应用中也应该放在 serivce 之中，
     * 并要对数据进正确性进行验证，在此仅为了偷懒
     */
    @Before(FlightticketsValidator.class)
    public void update() {
        Flighttickets flighttickets=getBean(Flighttickets.class);

        Flighttime flighttime=this.flightTimeService.findById(flighttickets.getFlighttimeid());

        flighttickets.setFlighttimeno(flighttime.getFlighttimeno());

        flighttickets.update();

        redirect("/flighttickets");
    }


    public void delete() {
        flightticketsService.deleteById(getParaToInt());
        redirect("/flighttickets");
    }
}
